<!-- <template>
	<view class="ALL">
		<view class="title">
			<text>绑定微信号</text>
		</view>
		<view class="vxlogo">
			<image src="../../static/img/weixin.png" mode=""></image>
		</view>
		<view class="text">
			<text>首次登录需要绑定微信号，</text>
			<text>点击下一步完成绑定</text>
		</view>
		<view class="ensure" @tap="loginin">
			<text>微信登录</text>
		</view>
	</view>
</template> -->
<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<!-- <view v-if="isCanUse"> -->
		<view>
			<view>
				<view class='header'>
					<image src='../../static/img/weixin@2x.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>

				<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
					授权登录
				</button>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<!-- <script>
	// import apis from '../../api.js'
	import {
		mapState,
		mapMutations
	}from 'vuex'  //导入vuex的mapState和mapMutations方法
	export default {
		data() {
			return {
				
			}
		},
		computed:{
			...mapState(['hasLogin']), //对全局变量hasLogin进行监控
		},
		methods: {
			...mapMutations(['login']), //对全局方法 login进行监控
			loginin(){
				this.login()
				uni.switchTab({
					url:'../index/index'
				})
			},
		}
	}
</script>
 -->

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex' //导入vuex的mapState和mapMutations方法
	
	import apis from '../../api.js'
	export default {
		data() {
			return {
				SessionKey: '',
				OpenId: '',
				nickName: null,
				avatarUrl: null,
				// isCanUse: uni.getStorageSync('isCanUse') || true, //默认为true
				code: ''
			};
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']), //对全局变量hasLogin进行监控
		},
		methods: {
			...mapMutations(['login','logout']), //对全局方法 login进行监控
			//第一授权获取用户信息===》按钮触发
			wxGetUserInfo() {
				let _this = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						console.log(infoRes)
						// let nickName = infoRes.userInfo.nickName; //昵称
						// let avatarUrl = infoRes.userInfo.avatarUrl; //头像
						// try {
							// uni.setStorageSync('isCanUse', false); //记录是否第一次授权  false:表示不是第一次授权
							_this.goto();
						// } catch (e) {}
					},
					fail(res) {}
				});
			}, //登录
			loginin() {
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				});

				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						_this.code = loginRes.code;
						// if (!_this.isCanUse) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) { 
									
								}
							});


						// }
						uni.hideLoading();
					},
				});
			},
			toIndex(provider) {	
				var that = this;
				that.login(provider)
				console.log('xxxxx---22---')
				uni.switchTab({
					url: '../index/index'
				})
			},
			goto() {
				var that = this;
				apis.login({
					"name": this.code,
					"type": 0
				}).then(res => {
					console.log(res)
					if(res[1].data.status==200){
						that.toIndex(res[1].data)
					}
				})

			}
		},
		onLoad() { //默认加载
			this.loginin();
		}
	}
</script>



<style scoped>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
